package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.ScanWirelessControllerBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/09/22
 * desc   : 房间标签数据列表
 */
public class SelectGatewayAdapter extends AppAdapter<ScanWirelessControllerBean.MasterController> {

    private ScanWirelessControllerBean.MasterController selectedMasterController;

    public SelectGatewayAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private TextView tvName;
        private TextView tvId;
        private CheckBox checkbox;

        private ViewHolder() {
            super(R.layout.item_select_gateway);
            tvName = (TextView) findViewById(R.id.tv_name);
            tvId = (TextView) findViewById(R.id.tv_id);
            checkbox = (CheckBox) findViewById(R.id.checkbox);
            checkbox.setOnClickListener(mCheckListener);
            itemView.setOnClickListener(mCheckListener);
        }

        @Override
        public void onBindView(int position) {
            ScanWirelessControllerBean.MasterController scanWirelessController = getItem(position);
            checkbox.setTag(getItem(position));
            tvName.setText(scanWirelessController.getName());
            tvId.setText("ID:" + scanWirelessController.getCodeNo());
            if (selectedMasterController != null && selectedMasterController.getControllerSerialNo().equals(scanWirelessController.getControllerSerialNo())){
                checkbox.setChecked(true);
            }else {
                checkbox.setChecked(false);
            }
        }
    }

    private View.OnClickListener mCheckListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox checkbox = null;
            if(v instanceof CheckBox){
                checkbox = (CheckBox) v;
            }else{
                checkbox = (CheckBox) v.findViewById(R.id.checkbox);
            }
            ScanWirelessControllerBean.MasterController scanWirelessController = (ScanWirelessControllerBean.MasterController) checkbox.getTag();
            if (checkbox.isChecked()){
                selectedMasterController = scanWirelessController;
                notifyDataSetChanged();
                onSelectChanged(selectedMasterController);
            }else {
                checkbox.setChecked(true);
            }
        }
    };

    public ScanWirelessControllerBean.MasterController getSelectedData() {
        return selectedMasterController;
    }

    public void setSelected(ScanWirelessControllerBean.MasterController scanWirelessController) {
        this.selectedMasterController = scanWirelessController;
    }

    public void onSelectChanged(ScanWirelessControllerBean.MasterController selected) {
    }
}